﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;

using CatUtils.Utils.SpecialUtils.Html;
using CatUtils.Utils.SpecialUtils.View;
using CatUtilsDll.Utils.Currency.WebUtils;
using RabbitDownload.Helper;
using RabbitDownload.Helper.Currency;

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace RabbitDownload.WebSite.Pixiv.PixivDic
{
    internal class PixivDicHelper
    {
        /// <summary>
        /// P站标签信息列表
        /// </summary>
        public static List<string> SelectList = new List<string> { "アニメ", "マンガ", "ラノベ", "ゲーム", "フィギュア", "音楽", "アート", "デザイン", "一般",
            "人物", "キャラクター", "セリフ", "イベント", "同人サークル" };

        /// <summary>
        /// 初始化选择方法类
        /// </summary>
        /// <param name="comboBox"></param>
        public void InitCombobox(ComboBox comboBox)
        {
            comboBox.Items.AddRange(SelectList.ToArray());
            comboBox.SelectedIndex = 0;
        }

        /// <summary>
        /// 初始化扫描功能
        /// </summary>
        /// <param name="ListName"></param>
        /// <param name="Start"></param>
        /// <param name="End"></param>
        /// <param name="ArticleCountStr">图片数量需要大于</param>
        public void InitScanner(string ListName, string Start, string End, string ArticleCountStr, bool AlertFlag = true)
        {
            int StartCount = TextHelper.StringToInt(Start);
            int EndCount = TextHelper.StringToInt(End);
            int ArticleCount = TextHelper.StringToInt(ArticleCountStr);
            List<string> TagList = GetAllTag(ListName, StartCount, EndCount, ArticleCount);
            string SavePath = StaticValueOld.DownloadPath + "PixivTag\\";
            FloderHelper.FloderExits(SavePath, true);
            FileHelper.WriteList(SavePath + TextHelper.ReplaceBadChar(ListName) + ".txt", TagList);
            if (AlertFlag)
                MessageHelper.OpenFloderAlert(SavePath);
        }

        /// <summary>
        /// 扫描P站所有数据项
        /// </summary>
        /// <param name="ArticleCountStr">图片数量需要大于</param>
        internal void ScannerAll(string ArticleCountStr)
        {
            /*需要扫描自动获取一共有多少数据*/
            foreach (var item in SelectList)
            {
                int PageCount = TextHelper.StringToInt(GetTagCount(item));
                Console.WriteLine(item + ":" + "PageCount:" + PageCount / 16);
                InitScanner(item, "1", (PageCount / 16).ToString(), ArticleCountStr, false);
            }
        }

        private string GetTagCount(string TagName)
        {
            string TagLink = "https://dic.pixiv.net/category/" + WebHelper.UrlEncode(TagName);
            Console.WriteLine(TagLink);
            string Shtml = WebHelper.HttpPcGet(TagLink);
            string CountText = TextHelper.RegexText(HtmlHelper.GetFirstText(Shtml, "//*[@id=\"search-title\"]/div"), "のカテゴリ：[0-9]{1,}").Replace("のカテゴリ：", "");

            return CountText;
        }

        /// <summary>
        /// 获取某个标签的结果
        /// </summary>
        /// <param name="ListName"></param>
        /// <param name="startCount"></param>
        /// <param name="endCount"></param>
        /// <param name="ArticleCount"></param>
        /// <returns></returns>
        private List<string> GetAllTag(string ListName, int startCount, int endCount, int ArticleCount)
        {
            string API = "https://dic.pixiv.net/category/" + WebHelper.UrlEncode(ListName) + "?page={0}";

            List<string> TempTag = new List<string>();
            for (; startCount < endCount; startCount++)
            {
                new PrintLog().PercentLog(endCount, startCount);
                string Shtml = WebHelper.HttpPcGet(string.Format(API, startCount));

                foreach (string Line in HtmlHelper.GetHtmlValueList(Shtml, "//article"))
                {
                    int Count = TextHelper.StringToInt(
                       HtmlHelper.GetFirstText(Line, "//ul/li[3]").Replace("作品数: ", "")

                        );
                    string Tag = HtmlHelper.GetFirstText(Line, "//h2");
                    Console.WriteLine("分类{2}标签{0},数量{1}", Tag, Count, ListName);
                    string ModeText = "<taginfo><tagname>{0}</tagname><tagcount>{1}</tagcount></taginfo>";
                    if (Count > ArticleCount)
                        TempTag.Add(string.Format(ModeText, Tag, Count));
                }
            }
            return TempTag;
        }
    }
}